$(function(){
	var part_a = [],part_b = [],operation_type;
	//书的选择下拉菜单
	$(".dp_ti").on("click",function(){
		$(this).parent().find("ul").toggle();
	})

	$(".book_choose .dp_li_rt span").on("click",function(){
		var add_book = $(this).parent().parent().find(".dp_li_lf");
		var add_book_name = add_book.html();
		var add_book_id = add_book.data("id");
		if($(this).html()=='A'){
			if(part_a.indexOf(add_book_id) == -1 && part_b.indexOf(add_book_id) == -1){
				$(".A_zone>.classify_box").append("<div class='add_book'><div class='add_book_name'>"+add_book_name+"</div><div class='add_book_remove' data-id="+add_book_id+"><span class='glyphicon glyphicon-remove'></span></div></div>");
				part_a.push(add_book_id);
				$(this).prop("className","chosen").siblings().prop("className","");
			}
		}
		if($(this).html()=='B'){
			if(part_a.indexOf(add_book_id) == -1 && part_b.indexOf(add_book_id) == -1){
				$(".B_zone>.classify_box").append("<div class='add_book'><div class='add_book_name'>"+add_book_name+"</div><div class='add_book_remove' data-id="+add_book_id+"><span class='glyphicon glyphicon-remove'></span></div></div>");
				part_b.push(add_book_id);
				$(this).prop("className","chosen").siblings().prop("className","");
			}
		}
		$(".add_book_remove").each(function(){
			$(this).on("click",function(){
				remove_id = $(this).data("id");
				$(".dp_li_lf[data-id="+remove_id+"]").parent().find("span").attr("class","");
				$(this).parent().remove();
				var a_index = part_a.indexOf($(this).data("id"));
				var b_index = part_b.indexOf($(this).data("id"));
				if(a_index >= 0){
					part_a.splice(a_index,1);
				}
				if(b_index >= 0){
					part_b.splice(b_index,1);
				}
			})
		})
	})
	//已保存的融合词表点击导出
	$(".exist_output").on("click",function(){
		var exist_id = $(this).parent().parent().find(".dp_li_lf").data("id");
		$.get("/out/"+exist_id,function(){
			window.location.href = '/out/' + exist_id;
		})
	})
	//点击操作类型
	$(".operation img").each(function(){
		$(this).on("click",function(){
			$(this).css("border","2px solid #900").parent().siblings().find("img").css("border","");
			operation_type = $(this).data("type");
			$("#form_logic").attr("src",$(this).attr("src"));
		})
	})
	//点击确认后弹出表单
	$(".confirm").on("click",function(){
		if(part_a.length == 0 || operation_type == undefined){
			$(".voc_logic_must").show();
		}
		else{
			$(".voc_logic_must").hide();
			$(".mask").show();
			$(".mask").css("height", document.body.scrollHeight);
			// 取消填写表单
			$(".form_cancel").on("click",function(){
				$(".mask").hide();
			});
		}

	})
	//保存后的提示
	function tip(type,content){
		$(".top_tip").removeClass("tip_wrong tip_right").addClass(type).html(content).css("top",$(window).scrollTop()).fadeIn(2000,function(){
			$(".top_tip").fadeOut(3000);
		})

	}
	// 保存新词表
	$(".save").each(function(){
		$(this).on("click",function(){
			var form_name = $("#form_name").val(),
				form_description = $("#form_description").val(),
				output = $(this).data("output"),
				name = $.trim($("#form_name").val()),
				params = {part_a:part_a,part_b:part_b,name:form_name,description:form_description,type:operation_type,output:output};
			if(name == ""){
				$(".form_tip").show();
			}
			else{
				$(".form_tip").hide();
				$.ajax({
					url: '/range',
					method: 'get',
					data: params,
					success: function(res) {
						if(res.status == 0){
							tip("tip_wrong","保存失败!")
						}
						else{
							if(res.status == 2){
								window.location.href = '/out/' + res.save_id;
							}
							if(res.status == 1){
								tip("tip_right","保存成功");
							}
							$("#form_name").val("");
							$("#form_description").val("");
							$(".mask").hide();
							$(".book_save ul").append('<li><div class="dp_li_lf" data-id='+res.save_id+'>'
							+res.save_name+'</div><div class="dp_li_rt"><span class="btn btn-primary exist_output">导出</span>'
							+'</div></li>')
							$(".exist_output").on("click",function(){
								var exist_id = $(this).parent().parent().find(".dp_li_lf").data("id");
								$.get("/out/"+exist_id,function(){
									window.location.href = '/out/' + exist_id;
								})
							})
						}
					}
				});
			}
		})
	})


})